package itheima.com.Day13.Test7;

public class Ticket implements Runnable {
    private static int ticketcount =100;
    @Override
    public void run() {
        while (true) {
            if("窗口一".equals(Thread.currentThread().getName())){
                boolean result = synchronizedMethod();
                if(result){
                    break;
                }
            }
            if("窗口二".equals(Thread.currentThread().getName())){
                synchronized (Ticket.class){
                    if(ticketcount==0){
                        break;
                    }else{
                        try {
                            Thread.sleep(10);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        ticketcount--;
                        System.out.println(Thread.currentThread().getName() + "还剩" + ticketcount + "张票在出售");
                    }
                }
            }
        }
    }

    private static synchronized boolean synchronizedMethod() {
        if(ticketcount==0){
            return true;
        }else {
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            ticketcount--;
            System.out.println(Thread.currentThread().getName() + "还剩" + ticketcount + "张票在出售");
            return false;
        }
    }
}
